pkgname=virttest
pkgver=git
pkgrel=1
arch=('i386' 'x86_64' 'aarch64')
url="https://gitee.com/openeuler/avocado-vt"
license=('GPL' 'GPL' 'GPL' 'GPL' 'GPL')
source=("https://gitee.com/openeuler/avocado.git" "https://gitee.com/openeuler/avocado-vt.git" "https://gitee.com/openeuler/tp-qemu.git" "https://gitee.com/openeuler/tp-libvirt.git" "https://gitee.com/openeuler/EulerRobot.git")
md5sums=('SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP') 

if [ x$qemu_san == x"enable" -a x$upstream_qemu_san == x"enable" ]; then
    upstream_qemu_san=""
fi

if [ x$qemu_san == x"enable" ]; then
    source=(${source[*]} "https://gitee.com/src-openeuler/qemu.git")
    license=(${license[*]} 'GPL')
    md5sums=(${md5sums[*]} 'SKIP')
fi

if [ x$upstream_qemu_san == x"enable" ]; then
    source=(${source[*]} "https://gitee.com/openeuler/qemu.git")
    license=(${license[*]} 'GPL')
    md5sums=(${md5sums[*]} 'SKIP')
fi

install_virttest() {
    local branch=$1

    cd "$srcdir/tp-qemu"
    git checkout -f "$branch"

    cd "$srcdir/tp-libvirt"
    git checkout -f "$branch"

    cd "$srcdir/avocado"
    git checkout -f "$branch"
    sed -i "s/^libvirt-python/#&/" requirements-selftests.txt
    pip3 install -r requirements-selftests.txt
    python3 setup.py install

    cd "$srcdir/avocado-vt"
    git checkout -f "$branch"
    sed -i "/^branch: /d" test-providers.d/io-github-autotest-qemu.ini
    sed -i "/^uri: /c uri: $srcdir/tp-qemu" test-providers.d/io-github-autotest-qemu.ini
    sed -i "/^uri: /a branch: $branch" test-providers.d/io-github-autotest-qemu.ini
    sed -i "/^branch: /d" test-providers.d/io-github-autotest-libvirt.ini
    sed -i "/^uri: /c uri: $srcdir/tp-libvirt" test-providers.d/io-github-autotest-libvirt.ini
    sed -i "/^uri: /a branch: $branch" test-providers.d/io-github-autotest-libvirt.ini
    rm -f test-providers.d/io-github-spiceqa-spice.ini
    pip3 install -r requirements.txt
    python3 setup.py install

    avocado vt-bootstrap --vt-type qemu --vt-skip-verify-download-assets --yes-to-all
    avocado vt-bootstrap --vt-type libvirt --vt-skip-verify-download-assets --yes-to-all
}

qemu_asan_build() {
    rpm_source_dir="/root/rpmbuild/SOURCES"

    if ! rpm -q rpm-build > /dev/null 2>&1; then
            yum install -y rpm-build || exit $?
    fi
    [ -d $rpm_source_dir ] && rm -rf $rpm_source_dir
    mkdir -p $rpm_source_dir
    [ ! -z "$qemu_san_branch" ] && cd ${srcdir}/qemu/ && git checkout -f $qemu_san_branch
    cp -a ${srcdir}/qemu/* $rpm_source_dir
    qemu_spec=${srcdir}/qemu/qemu.spec
    sed -i '/--python=/a \    --enable-sanitizers \\' ${qemu_spec}
    sed -i 's/^make check/# &/' ${qemu_spec}
    sed -i 's/^make %{_smp_mflags} \$buildldflags/make -j 60 \$buildldflags/' ${qemu_spec}

    sed -i 's/^%__check_files/# &/' /usr/lib/rpm/macros

    yum builddep -y ${qemu_spec} || exit $?
    rpmbuild -ba ${qemu_spec} || exit $?
}

install_upstream_qemu_depends() {
    mkdir $TMP/qemu_depends/
    sudo yum install -y \
        ninja-build \
        diffutils \
        gcc \
        git \
        make \
        bison \
        brlapi-devel \
        bzip2-devel \
        chrpath \
        cyrus-sasl-devel \
        device-mapper-multipath-devel \
        flex \
        gettext \
        gnutls-devel \
        gtk3-devel \
        kernel \
        libaio-devel \
        libattr-devel \
        libcap-devel \
        libcap-ng-devel \
        libcurl-devel \
        libepoxy-devel \
        libfdt-devel \
        libiscsi-devel \
        libjpeg-devel \
        libpng-devel \
        librbd-devel \
        libseccomp-devel \
        libtasn1-devel \
        libudev-devel \
        libusbx-devel \
        libxml2-devel \
        lzo-devel \
        ncurses-devel \
        numactl-devel \
        pam-devel \
        perl-Test-Harness \
        perl-podlators \
        pixman-devel \
        python-sphinx \
        python3-devel \
        rdma-core-devel \
        snappy-devel \
        systemd-devel \
        texinfo \
        usbredir-devel \
        virglrenderer-devel \
        zlib-devel \
        libusb-devel \
        xfsprogs-devel \
        device-mapper-devel \
        libgcrypt-devel \
        libasan \
        --downloadonly --downloaddir=$TMP/qemu_depends
    sudo rpm -ivh --force --nodeps $TMP/qemu_depends/*.rpm
}

build_upstream_qemu_asan() {
    cd ${srcdir}/qemu
    git checkout -f master
    sed -i 's/git.qemu.org\/git/gitee.com\/EulerRobot/g' .gitmodules
    git submodule update --init --recursive capstone
    git submodule update --init --recursive dtc
    git submodule update --init --recursive meson
    git submodule update --init --recursive slirp
    git submodule update --init --recursive tests/fp/berkeley-softfloat-3
    git submodule update --init --recursive tests/fp/berkeley-testfloat-3
    git submodule update --init --recursive ui/keycodemapdb
    mkdir openEuler-linux-gnu
    cd openEuler-linux-gnu

    ../configure \
        --extra-cflags="-O2 -g -pipe -Wall -Wno-error -Wimplicit-fallthrough=0 -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -fasynchronous-unwind-tables -fstack-clash-protection -fPIE -DPIE -fPIC" \
        --enable-modules \
        --enable-qom-cast-debug \
        --enable-vnc \
        --enable-debug-tcg \
        --enable-debug \
        --enable-vnc-sasl \
        --enable-vnc-jpeg \
        --enable-vnc-png \
        --enable-kvm \
        --enable-linux-user \
        --enable-xkbcommon \
        --enable-vhost-user \
        --enable-vhost-kernel \
        --enable-sheepdog \
        --enable-parallels \
        --enable-qed \
        --enable-vvfat \
        --enable-vdi \
        --enable-qcow1 \
        --enable-dmg \
        --enable-cloop \
        --enable-bochs \
        --enable-replication \
        --enable-libxml2 \
        --enable-numa \
        --enable-live-block-migration \
        --enable-tpm \
        --enable-pvrdma \
        --enable-rdma \
        --enable-auth-pam \
        --enable-gcrypt \
        --enable-gnutls \
        --enable-tools \
        --enable-guest-agent \
        --enable-bzip2 \
        --enable-libusb \
        --enable-hax \
        --enable-cap-ng \
        --enable-libiscsi \
        --enable-gtk \
        --enable-bsd-user \
        --enable-curses \
        --enable-iconv \
        --enable-curl \
        --enable-fdt \
        --enable-linux-aio \
        --enable-attr  \
        --enable-crypto-afalg \
        --enable-vhost-net \
        --enable-vhost-crypto \
        --enable-vhost-scsi \
        --enable-vhost-vsock \
        --enable-vhost-user-fs \
        --enable-xfsctl \
        --enable-libusb \
        --enable-guest-agent \
        --enable-plugins \
        --enable-xkbcommon \
        --enable-capstone \
        --enable-live-block-migration \
        --enable-virtfs \
        --enable-sanitizers \
        || (cat config.log; exit 1)

    make -j 60
}

build() {
    install_virttest "openEuler-20.03"
    if [ x$qemu_san == x"enable" ]; then
        qemu_asan_build
    fi
    if [ x$upstream_qemu_san == x"enable" ]; then
        install_upstream_qemu_depends
        build_upstream_qemu_asan
    fi
}

avocado_copy() {
    mkdir -p "${pkgdir}/lkp/benchmarks"
    cp -rf "/usr/local/bin/avocado" "${pkgdir}/lkp/benchmarks"
    mkdir -p "${pkgdir}/usr/local/bin/"
    cp -rf "/usr/local/bin/"* "${pkgdir}/usr/local/bin/"
    mkdir -p "${pkgdir}/usr/local/lib"
    cp -rf "/usr/local/lib/"* "${pkgdir}/usr/local/lib/"
    mkdir -p "${pkgdir}/usr/share/"
    cp -rf "/usr/share/avocado-plugins-vt" "${pkgdir}/usr/share/"
    mkdir -p "${pkgdir}/var/lib/avocado"
    cp -rf "/var/lib/avocado/"* "${pkgdir}/var/lib/avocado/"
    mkdir -p "{$pkgdir}/root/avocado/job-results/"
}

lkp_virttest_copy() {
    mkdir -p "${pkgdir}/${LKP_SRC}/distro/depends"
    cp -rf "${srcdir}/EulerRobot/distro/depends/"* "${pkgdir}/${LKP_SRC}/distro/depends/"
    mkdir -p "${pkgdir}/${LKP_SRC}/pkg/virttest"
    cp -rf "${srcdir}/EulerRobot/pkg/virttest"* "${pkgdir}/${LKP_SRC}/pkg/virttest/"
    mkdir -p "${pkgdir}/${LKP_SRC}/stats"
    cp -rf "${srcdir}/EulerRobot/stats/"* "${pkgdir}/${LKP_SRC}/stats/"
    mkdir -p "${pkgdir}/${LKP_SRC}/tests"
    cp -rf "${srcdir}/EulerRobot/tests/"* "${pkgdir}/${LKP_SRC}/tests/"
    mkdir -p "${pkgdir}/${LKP_SRC}/virttest"
    cp -rf "${srcdir}/EulerRobot/config/vmxml/"* "${pkgdir}/${LKP_SRC}/virttest/"
}

san_rpm_copy() {
    mkdir -p "${pkgdir}/virttest_san_rpm/"
    cp -rf "/root/rpmbuild/RPMS/aarch64/"* "${pkgdir}/virttest_san_rpm/"
    cp -rf "/root/rpmbuild/RPMS/noarch/"* "${pkgdir}/virttest_san_rpm/"
}

upstream_qemu_copy() {
    mkdir -p "${pkgdir}/usr/libexec/upstream_qemu"
    cp -a "${srcdir}/qemu/openEuler-linux-gnu/qemu-system-$(arch)" "${pkgdir}/usr/libexec/upstream_qemu"
    cp -a "${srcdir}/qemu/openEuler-linux-gnu/qemu-img" "${pkgdir}/usr/libexec/upstream_qemu"
}

package() {
    lkp_virttest_copy
    avocado_copy
    if [ x$qemu_san == x"enable" ]; then
        san_rpm_copy
    fi
    if [ x$upstream_qemu_san == x"enable" ]; then
        upstream_qemu_copy
    fi
}

